﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System.Threading
{
	internal sealed class LockCookieHelper : IDisposable
	{

		private LockMode mode;
		private ReaderWriterLockSlim handle;

		public LockCookieHelper( ReaderWriterLockSlim handle, LockMode mode )
		{
			this.mode = mode;
			this.handle = handle;
		}

		public void Dispose()
		{
			switch ( this.mode )
			{
				case LockMode.Read:
					this.handle.ExitReadLock();
					break;
				case LockMode.Write:
					this.handle.ExitWriteLock();
					break;
				case LockMode.UpgradeToWrite:
					this.handle.ExitUpgradeableReadLock();
					break;
				default:
					throw new InvalidOperationException();
			}
		}
	}
}
